package com.hlkj.pay.controller.common;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.app.common.IUploadService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.enums.CommonEnum;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "上传文件管理")
@RestController
@RequestMapping("/v1/common/upload")
@Slf4j
@RequiredArgsConstructor
public class UploadController {

    private final IUploadService uploadService;

    @PostMapping("/image")
    @Operation(summary ="上传图片")
    public CommonResult<String> image(HttpServletRequest request) {
        MultipartFile multipartFile;
        try {
            multipartFile = ((MultipartRequest) request).getFile("file");
        } catch (Exception e) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }

        if (multipartFile == null) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }
        return uploadService.upload(multipartFile, CommonEnum.OssEnum.IMAGE);
    }

    @PostMapping("/video")
    @Operation(summary ="上传视频")
    public CommonResult<String> video(HttpServletRequest request) {
        MultipartFile multipartFile;
        try {
            multipartFile = ((MultipartRequest) request).getFile("file");
        } catch (Exception e) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }

        if (multipartFile == null) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }
        return uploadService.upload(multipartFile, CommonEnum.OssEnum.VIDEO);
    }
    @PostMapping("/file")
    @Operation(summary ="上传文件")
    public CommonResult<String> file(HttpServletRequest request) {
        MultipartFile multipartFile;
        try {
            multipartFile = ((MultipartRequest) request).getFile("file");
        } catch (Exception e) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }

        if (multipartFile == null) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }
        return uploadService.upload(multipartFile, CommonEnum.OssEnum.FILE);
    }
    
    @PostMapping("/secret")
    @Operation(summary ="上传秘钥等文件")
    public CommonResult<String> secret(HttpServletRequest request) {
        MultipartFile multipartFile;
        try {
            multipartFile = ((MultipartRequest) request).getFile("file");
        } catch (Exception e) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }
        
        if (multipartFile == null) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }
        return uploadService.uploadSecret(multipartFile);
    }

}
